/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-12-12
 * Time: 19:30
 */
import java.util.ArrayList;
import java.util.List;
public class Library {

    // Add the missing implementation to this class

    public String address;
    public List<Book> books;
    public Library(String address) {
        books = new ArrayList<Book>();
        this.address = address;
    }

    public static void main(String[] args) {
        // Create two libraries
        Library firstLibrary = new Library("文化路80号");
        Library secondLibrary = new Library("建设路22号");
        // Add four books to the first library
        firstLibrary.addBook(new Book("飞鸟集", "泰戈尔"));
        firstLibrary.addBook(new Book("安妮日记", "弗兰克"));
        firstLibrary.addBook(new Book("红楼梦", "曹雪芹"));
        firstLibrary.addBook(new Book("希腊的神话和传说", "斯威布"));
        secondLibrary.addBook(new Book("安妮日记", "弗兰克"));
        secondLibrary.addBook(new Book("红楼梦", "曹雪芹"));
        secondLibrary.addBook(new Book("希腊的神话和传说", "斯威布"));
        System.out.println("图书馆地址:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();
        System.out.println("从两个图书馆借【飞鸟集】");
        firstLibrary.borrowBook("飞鸟集");
        secondLibrary.borrowBook("飞鸟集");
        System.out.println();
        System.out.println("first library可外借的图书:");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("second library可外借的图书:");
        secondLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("向first library归还【飞鸟集】:");
        firstLibrary.returnBook("飞鸟集");
        System.out.println();
        // 输出first library图书馆可外借的图书
        System.out.println("first library可外借的图书:");
        firstLibrary.printAvailableBooks();
    }
    private void returnBook(String string) {
        //遍历书籍并更改所还书的状态
        for (Book book : books) {
            if (book.title.equals(string)) {
                book.borrowed = false;
            }
        }
    }
    void printAvailableBooks() {
        //遍历books中所有书籍
        for (Book book : books) {
            if (book.isAvailable())//如果书籍可用
                System.out.println("图书名称："
                        + book.title + "  作者：" + book.author);
        }
    }
    private void addBook(Book book) {
        books.add(book);//调用add方法添加书籍
    }
    private void borrowBook(String string) {
        boolean has = false;// 记录是否有要借的书
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.title.equals(string)) {
                has = true;// 找到此书，判断是否借出
                if (book.isAvailable()) {
                    // 借出
                    book.borrowed = true;
                    System.out.println("成功借阅" + book.title);//提示信息
                } else {
                    System.out.println("该图书已经借出！");
                }
            }
        }
        //若没有找到指定书籍，输出提示信息
        if (!has)
            System.out.println("没有找到" + string + "！");
    }
    private void printAddress() {
        System.out.println(address);
    }
}